package com.example.scity;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.scity.beens.MessageBeen;
import com.example.scity.beens.SetMsgBeen;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class GrzxActivity extends AppCompatActivity {
    MessageBeen messageBeen;
    SetMsgBeen setMsgBeen;
    Button button;
    EditText ed_nick;
    ImageView img_avater;
    EditText ed_phone;
    EditText ed_email;
    EditText ed_id_card;
    RadioGroup radioGroup;
    RadioButton rb_male;
    RadioButton rb_female;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Glide.with(GrzxActivity.this).load(Configs.Host + messageBeen.user.avatar);
                    ed_nick.setText(messageBeen.user.nickName);
                    ed_phone.setText(messageBeen.user.phonenumber);
                    ed_email.setText(messageBeen.user.email);
                    ed_id_card.setText(messageBeen.user.idCard);
                    String sex = messageBeen.user.sex;
                    if (sex.equals("0")) {
                        radioGroup.check(R.id.rd_male);
                    } else {
                        radioGroup.check(R.id.rd_female);
                    }
                    break;
                case 1:
                    Toast.makeText(GrzxActivity.this, setMsgBeen.msg, Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grzx);
        initView();
        LoadMessage();
        setMessage();
    }
    private void setMessage() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("email", ed_email.getText().toString());
                map.put("idCard", ed_id_card.getText().toString());
                map.put("nickName", ed_nick.getText().toString());
                map.put("phonenumber", ed_phone.getText().toString());
                String sex2;
                if (rb_male.isChecked()) {
                    sex2 = "0";
                } else {
                    sex2 = "1";
                }
                map.put("sex", sex2);
                OkHttpClient okHttpClient = new OkHttpClient();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody requestBody = RequestBody.create(mediaType, new Gson().toJson(map));
                Request request = new Request.Builder()
                        .url(Configs.Host + APIInterface.SetMessage)
                        .addHeader("Authorization", Configs.Token)
                        .put(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        setMsgBeen = new Gson().fromJson(response.body().string(), SetMsgBeen.class);
                        handler.sendEmptyMessage(1);
                    }
                });
            }
        });
    }

    @SuppressLint("WrongViewCast")
    private void initView() {
        button = findViewById(R.id.button_update_user);
        img_avater = findViewById(R.id.ava_img);
        ed_nick = findViewById(R.id.et_nick);
        ed_phone = findViewById(R.id.et_tel);
        ed_email = findViewById(R.id.editText_email);
        ed_id_card = findViewById(R.id.editText_id_card);
        radioGroup = findViewById(R.id.grxx_radiogroup);
        rb_male= findViewById(R.id.rd_male);
        rb_female = findViewById(R.id.rd_female);
    }
    private void LoadMessage() {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(Configs.Host + APIInterface.GetMessage)
                .addHeader("Authorization", Configs.Token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                messageBeen = new Gson().fromJson(response.body().string(), MessageBeen.class);
                handler.sendEmptyMessage(0);//加载成功
            }
        });
    }
}